package com.sec.android.app.myfiles.facade.cmd;

import android.content.Context;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.common.Configs;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.securefolder.HideUtils;
import com.sec.android.app.myfiles.util.securefolder.HideUtilsV2;
import com.sec.android.app.myfiles.util.securefolder.SecureConstants;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hyl on 2018/6/2.
 */

public class RemoveFromPrivateFolderCmd extends SimpleCommand{

    private static final String TAG = RemoveFromPrivateFolderCmd.class.getSimpleName();


    private int mProcessId;
    private Context mContext;
    private AbsMyFilesFragment mFragment;
    private ArrayList<FileRecord> mSrcRecordList;
    private String mPrivateRoot;

    @SuppressWarnings("unchecked")
    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        mProcessId = (Integer) params[0];
        mContext = (Context) params[1];
        mFragment = (AbsMyFilesFragment) params[2];
        mSrcRecordList = (ArrayList<FileRecord>) params[3];

        mPrivateRoot = SecureConstants.privateRecoveryRootPath;

        RemoveFromPrivateFolder(mSrcRecordList);
    }

    private void RemoveFromPrivateFolder(List<FileRecord> recordList) {
        Log.i(TAG, "RemoveFromPrivateFolder. ");
        if (recordList != null && !recordList.isEmpty()) {
            Log.i(TAG, "RemoveFromPrivateFolder. record count is " + recordList.size());
            for (FileRecord fileRecord : recordList) {
                if (fileRecord != null) {
                    Log.i(TAG, "RemoveFromPrivateFolder path: " + fileRecord.getFullPath());
                    File oldFile = new File(fileRecord.getFullPath());
                    if (Configs.HIDE_UTILS_V2) {
                        HideUtilsV2.getInstance(mContext).removeFromPrivateFolder(oldFile);
                    } else {
                        HideUtils.getInstance(mContext).removeFromPrivateFolder(oldFile);
                    }
                }
            }
        }
    }

    private String makeMessage(int privateItemCount, int normalItemCount) {
        String msg = "";

        if (normalItemCount > 1) {
            msg = mContext.getResources().getQuantityString(R.plurals.n_itmes_moved_to_private, privateItemCount, normalItemCount);
        } else {
            msg = mContext.getResources().getQuantityString(R.plurals.one_item_moved_to_private, privateItemCount);
        }

        return msg;
    }
}
